import api from '@/api';

export default {
  namespace: 'sourceManage',

  state: {
    sources: {
      list: [],
      pagination: {},
    },
    accounts: {
      list: [],
      pagination: {}
    },
    cusContacts: {
      list: [],
      pagination: {}
    },
    bizContacts: {
      list: [],
      pagination: {}
    },
    currentSource: {},
    authStatus: [],
    ratings: [],
    sourceInfo: {},
    importResult: {
      correct: [],
      incorrect: []
    },
    messageType: [],
    messageSubType: [],
    supplierList: {
      list: [],
      pagination: {}
    }
  },

  effects: {
    *approve({ payload }, { call }) {
      return yield call(api.sourceManage.approve, payload);
    },
    *closeShop({ payload }, { call }) {
      return yield call(api.sourceManage.closeShop, payload);
    },
    *openShop({ payload }, { call }) {
      return yield call(api.sourceManage.openShop, payload);
    },
    *commitAuth({ payload }, { call }) {
      return yield call(api.sourceManage.commitAuth, payload);
    },
    *queryMessage({ payload }, { call }) {
      return yield call(api.sourceManage.queryMessage, payload);
    },
    *sendMessage({ payload }, { call }) {
      return yield call(api.sourceManage.sendMessage, payload);
    },
    *messageType({ payload }, { call, put }) {
      const { errorCode, data } = yield call(api.sourceManage.messageType, payload);
      if (errorCode === 0) {
        yield put({
          type: 'saveMessageType',
          payload: {
            messageType: data
          }
        })
      }
    },
    *messageSubType({ payload }, { call, put }) {
      const { errorCode, data } = yield call(api.sourceManage.messageSubType, payload);
      if (errorCode === 0) {
        yield put({
          type: 'saveMessageSubType',
          payload: {
            messageSubType: data
          }
        })
      }
    },
    *applySupplier({ payload }, { call }) {
      return yield call(api.sourceManage.applySupplier, payload);
    },

    *fetch({ payload }, { call, put }) {
      const response = yield call(api.sourceManage.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.sourceManage.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response;
    },
    *fetchCompany({ payload }, { call, put }) {
      return yield call(api.sourceManage.queryCompany, payload);
    },
    *add({ payload }, { call }) {
      return yield call(api.sourceManage.create, payload);
    },
    *update({ payload }, { call }) {
      return yield call(api.sourceManage.update, payload);
    },
    *delete({ payload }, { call }) {
      return yield call(api.sourceManage.remove, payload);
    },
    *addCompany({ payload }, { call }) {
      return yield call(api.sourceManage.updateCompany, payload);
    },
    *fetchAccounts({ payload }, { call, put }) {
      let res = yield call(api.sourceManage.queryAccounts, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAccounts',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
    },
    *lockAccount({ payload }, { call }) {
      return yield call(api.sourceManage.lockAccount, payload);
    },
    *unlockAccount({ payload }, { call }) {
      return yield call(api.sourceManage.unlockAccount, payload);
    },
    *addAccount({ payload }, { call }) {
      return yield call(api.sourceManage.createAccount, payload);
    },
    *updateAccount({ payload }, { call }) {
      return yield call(api.sourceManage.updateAccount, payload);
    },
    *resetPwd({ payload }, { call }) {
      return yield call(api.sourceManage.resetPwd, payload);
    },
    *removeAccounts({ payload }, { call }) {
      return yield call(api.sourceManage.removeAccounts, payload);
    },
    *fetchCusContacts({ payload }, { call, put }) {
      let res = yield call(api.sourceManage.queryCusContacts, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCusContacts',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
    },
    *addCusContact({ payload }, { call }) {
      return yield call(api.sourceManage.createCusContact, payload);
    },
    *updateCusContact({ payload }, { call }) {
      return yield call(api.sourceManage.updateCusContact, payload);
    },
    *removeCusContacts({ payload }, { call }) {
      return yield call(api.sourceManage.removeCusContacts, payload);
    },
    *fetchBizContacts({ payload }, { call, put }) {
      let res = yield call(api.sourceManage.queryBizContacts, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveBizContacts',
          payload: {
            list: res.data,
            pagination: res.pagination
          }
        });
      }
    },
    *addBizContact({ payload }, { call }) {
      return yield call(api.sourceManage.createBizContact, payload);
    },
    *updateBizContact({ payload }, { call }) {
      return yield call(api.sourceManage.updateBizContact, payload);
    },
    *removeBizContacts({ payload }, { call }) {
      return yield call(api.sourceManage.removeBizContacts, payload);
    },
    *audit({ payload }, { call }) {
      return yield call(api.sourceManage.audit, payload);
    },
    *queryCustomerInfo({ payload }, { call, put }) {
      const response = yield call(api.sourceManage.sourceInfo, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCustomerInfo',
          payload: response.data,
        });
      }
    },
    *queryImportResult({ payload }, { call, put }) {
      const response = yield call(api.sourceManage.importResult, payload);
      response.data = response.data || [];
      if (response.errorCode === 0) {
        yield put({
          type: 'saveImportResult',
          payload: {
            correct: response.data.filter(item => item.status === 1),
            incorrect: response.data.filter(item => item.status === 0),
          },
        });
      }
      return response.errorCode;
    },
    *saveExcelData({ payload }, { call }) {
      const response = yield call(api.sourceManage.saveExcelData, payload);
      return response;
    },
    *fetchOperateLog({ payload }, { call }) {
      const response = yield call(api.sourceManage.operateLog, payload);
      return response;
    },
    *fetchSupplierList({ payload }, { call, put }) {
      const response = yield call(api.sourceManage.querySupplierList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveSupplierList',
          payload: response,
        });
      }
    } 
  },

  reducers: {
    saveMessageSubType(state, { payload }) {
      return { ...state, ...payload }
    },
    saveMessageType(state, { payload }) {
      return { ...state, ...payload }
    },
    save(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        sources: {
          list: data,
          pagination
        },
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentSource: payload
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentSource: {}
      }
    },
    saveAccounts(state, { payload }) {
      return {
        ...state,
        accounts: payload
      };
    },
    saveCusContacts(state, { payload }) {
      return {
        ...state,
        cusContacts: payload
      };
    },
    saveBizContacts(state, { payload }) {
      return {
        ...state,
        bizContacts: payload
      };
    },
    saveCustomerInfo(state, { payload }) {
      return {
        ...state,
        sourceInfo: payload
      }
    },
    saveImportResult(state, { payload }) {
      return {
        ...state,
        importResult: payload
      }
    },
    saveSupplierList(state, { payload }) {
      const { data, pagination } = payload;
      return {
        ...state,
        supplierList: {
          list: data,
          pagination
        },
      };
    }
  }
};
